import subprocess

subprocess.call("wg genkey | tee /etc/wireguard/privatekey | wg pubkey | tee /etc/wireguard/publickey", shell=True)
subprocess.call("wg genkey | tee /etc/wireguard/naruto_priv | wg pubkey | tee /etc/wireguard/naruto_pub", shell=True)
subprocess.call("wg genkey | tee /etc/wireguard/hinata_priv | wg pubkey | tee /etc/wireguard/hinata_pub", shell=True)
subprocess.call("wg genkey | tee /etc/wireguard/sasuke_priv | wg pubkey | tee /etc/wireguard/sasuke_pub", shell=True)
subprocess.call("wg genkey | tee /etc/wireguard/sakura_priv | wg pubkey | tee /etc/wireguard/sakura_pub", shell=True)
subprocess.call("wg genkey | tee /etc/wireguard/kakashi_priv | wg pubkey | tee /etc/wireguard/kakashi_pub", shell=True)

privatekey = subprocess.check_output("cat /etc/wireguard/privatekey", shell=True, text=True)
naruto_pub = subprocess.check_output("cat /etc/wireguard/naruto_pub", shell=True, text=True)
hinata_pub = subprocess.check_output("cat /etc/wireguard/hinata_pub", shell=True, text=True)
sasuke_pub = subprocess.check_output("cat /etc/wireguard/sasuke_pub", shell=True, text=True)
sakura_pub = subprocess.check_output("cat /etc/wireguard/sakura_pub", shell=True, text=True)
kakashi_pub = subprocess.check_output("cat /etc/wireguard/kakashi_pub", shell=True, text=True)

text = (f"[Interface]\n"
f"Privatekey = {privatekey}"
f"ListenPort = 51820\n"
f"PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\n"
f"PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE\n"
f"\n"
f"[Peer]\n"
f"Publickey = {naruto_pub}"
f"AllowedIPs = 10.8.0.2/32\n"
f"\n"
f"[Peer]\n"
f"Publickey = {hinata_pub}"
f"AllowedIPs = 10.8.0.3/32\n"
f"\n"
f"[Peer]\n"
f"Publickey = {sasuke_pub}"
f"AllowedIPs = 10.8.0.4/32\n"
f"\n"
f"[Peer]\n"
f"Publickey = {sakura_pub}"
f"AllowedIPs = 10.8.0.5/32\n"
f"\n"
f"[Peer]\n"
f"Publickey = {kakashi_pub}"
f"AllowedIPs = 10.8.0.6/32\n"
)

subprocess.call(f"echo '{text}'> /etc/wireguard/wg0.conf", shell=True)

subprocess.call("systemctl enable wg-quick@wg0.service", shell=True)
subprocess.call("systemctl start wg-quick@wg0.service", shell=True)
subprocess.call("echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf", shell=True)

ipaddress = input("--Input server ip address:")
keeptime = input("--Input needed PersistentKeepAlive:")

publickey= subprocess.check_output("cat /etc/wireguard/publickey", shell=True, text=True)
naruto_priv = subprocess.check_output("cat /etc/wireguard/naruto_priv", shell=True, text=True)
hinata_priv =subprocess.check_output("cat /etc/wireguard/hinata_priv", shell=True, text=True)
sasuke_priv = subprocess.check_output("cat /etc/wireguard/sasuke_priv", shell=True, text=True)
sakura_priv = subprocess.check_output("cat /etc/wireguard/sakura_priv", shell=True, text=True)
kakashi_priv = subprocess.check_output("cat /etc/wireguard/kakashi_priv", shell=True, text=True)

subprocess.call("mkdir /etc/wireguard/configs", shell=True)

count = 2
config = (f"[Interface]\n"
f"Privatekey = {real_priv}\n"
f"Address = 10.8.0.{count}/32\n"
f"\n"
f"[Peer]\n"
f"Publickey = {publickey}\n"
f"AllowedIPs = 0.0.0.0/0\n"
f"Endpoint = {ipaddress}:51820\n"
f"PersistentKeepalive = {keeptime}\n"
)

while True:
    if count == 2:
        real_priv = naruto_priv
    if count == 3:
        real_priv = hinata_priv
    if count == 4:
        real_priv = sasuke_priv
    if count == 5:
        real_priv = sakura_priv
    if count == 6:
        real_priv = kakashi_priv 
    if count == 7:
        break
    subprocess.call(f"echo '{config}' > /etc/wireguard/configs/{count}.conf ", shell=True)
    count += 1

subprocess.call("sysctl -p", shell=True)

print("---All good!---\nConfigs was created check it in dir 'wireguard/configs'")
